package com.tabjin.rfidsocket.controller.v1.sys;

import com.tabjin.rfidsocket.authority.shiro.JwtUtil;
import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.exception.ParamException;
import com.tabjin.rfidsocket.pojo.SysUser;
import com.tabjin.rfidsocket.pojo.bo.SysUserBO;
import com.tabjin.rfidsocket.pojo.vo.SysRoleVO;
import com.tabjin.rfidsocket.pojo.vo.SysUserVO;
import com.tabjin.rfidsocket.service.sys.SysRoleService;
import com.tabjin.rfidsocket.service.sys.SysRoleUserService;
import com.tabjin.rfidsocket.service.sys.SysTreeService;
import com.tabjin.rfidsocket.service.sys.SysUserService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @author tabjin
 * create at 2020/12/9 16:10
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping(value = "/sys/user")
public class SysUserController {

    private final SysUserService sysUserService;
    private final SysTreeService sysTreeService;
    private final SysRoleService sysRoleService;
    private final SysRoleUserService sysRoleUserService;
    private final JwtUtil jwtUtil;

    public SysUserController(SysRoleService sysRoleService, SysTreeService sysTreeService, SysUserService sysUserService, SysRoleUserService sysRoleUserService1, JwtUtil jwtUtil) {
        this.sysRoleService = sysRoleService;
        this.sysTreeService = sysTreeService;
        this.sysUserService = sysUserService;
        this.sysRoleUserService = sysRoleUserService1;
        this.jwtUtil = jwtUtil;
    }

    /**
     * 无权限访问
     *
     * @return
     */
    @RequestMapping(value = "/noAuth.page")
    public ModelAndView noAuth() {
        return new ModelAndView("noAuth");
    }

    /**
     * 新增用户
     *
     * @param sysUserBO
     * @return TabJsonResult
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"/sys/user/add"}, logical = Logical.OR)
    public TabJsonResult add(@RequestBody SysUserBO sysUserBO) {
        SysUserVO vo = sysUserService.add(sysUserBO);
        return TabJsonResult.ok(vo);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"/sys/user/delete"}, logical = Logical.OR)
    public TabJsonResult add(@RequestParam(value = "userId", required = true) Integer userId) {
        Optional<Integer> userIdOptinal = sysUserService.delete(userId);
        Integer result = userIdOptinal.orElseThrow(() -> new ParamException("删除用户返回userId空"));
        return TabJsonResult.ok(result);
    }

    /**
     * 更新用户
     *
     * @param sysUserBO
     * @return TabJsonResult
     */
    @RequiresPermissions(value = {"/sys/user/update"})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody SysUserBO sysUserBO) {
        SysUserVO vo = sysUserService.update(sysUserBO);
        return TabJsonResult.ok(vo);
    }

    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
//    @RequiresPermissions(value = {"/sys/user/list"}, logical = Logical.OR)
    public TabJsonResult info(@RequestParam(value = "token", required = true) String token) {
        Integer userId = jwtUtil.getUserId(token);
        Optional<SysUser> sysUserOptional = sysUserService.findOne(userId);
        Optional<List<SysRoleVO>> roleListByUserIdOptional = Optional.ofNullable(sysRoleService.getRoleListByUserId(userId));
        List<SysRoleVO> sysRoleVOList = roleListByUserIdOptional.orElseThrow(() -> new ParamException("该用户无角色"));
        SysUser sysUser = sysUserOptional.orElseThrow(() -> new ParamException("无用户"));
        Map<String, Object> map = new HashMap<>();
        map.put("pureUser", sysUser);
        map.put("roles", sysRoleVOList);
        return TabJsonResult.ok(map);
    }

    /**
     * 获取用户权限数据和角色
     *
     * @param userId
     * @return TabJsonResult
     */
    @RequestMapping(value = "/acl", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"/sys/user/list"}, logical = Logical.OR)
    public TabJsonResult acl(@RequestParam(value = "userId", required = true) Integer userId) {
        jwtUtil.createToken(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("acl", sysTreeService.userAclTree(userId));
        map.put("role", sysRoleService.getRoleListByUserId(userId));// 用户已分配的角色
        return TabJsonResult.ok(map);
    }

    @RequiresPermissions(value = {"/sys/user/list"}, logical = Logical.OR)
    @RequestMapping(value = "/aclName", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult aclNameByUserId(@RequestParam(value = "userId", required = true) Integer userId) {
        jwtUtil.createToken(userId);
        sysTreeService.userAclNamesByUserId(userId);
        return TabJsonResult.ok();
    }

    /**
     * 获取所有用户
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequiresPermissions(value = {"/sys/user/list"}, logical = Logical.OR)
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult all(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PagedGridResult result = sysUserService.getAll(page, pageSize);
        return TabJsonResult.ok(result);
    }

    /**
     * 根据部门获取用户
     *
     * @param deptId
     * @return
     */
    @RequiresPermissions(value = {"/sys/user/list"}, logical = Logical.OR)
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult usersWithDeptId(@RequestParam(value = "deptId") Integer deptId) {
        List<SysUser> result = sysUserService.getUsersByDeptId(deptId);
        return TabJsonResult.ok(result);
    }

    /**
     * 用户状态切换
     *
     * @param
     * @return
     */
    @RequiresPermissions(value = {"/sys/user/update"}, logical = Logical.OR)
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult activeUser(@RequestParam(value = "id") Integer userId,
                                    @RequestParam(value = "status") Integer status) {
        sysUserService.activeUser(userId, status);
        return TabJsonResult.ok();
    }
}
